package cn.yuanqiao.web.controller.license.controller;

import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.license.YanzhengService;
import cn.yuanqiao.web.controller.license.utils.CreateZhucema;
import cn.yuanqiao.web.controller.license.utils.GenerateQR;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Map;

@RestController
@RequestMapping("/archive/license")
public class ArchiveLicenseController {
    private static final Logger log = LoggerFactory.getLogger(ArchiveLicenseController.class);

    @Value("${YQDA_LICENSE}")
    private String licenseFile;

    @PostMapping("/uploadLicense")
    public AjaxResult uploadLicense(MultipartFile file){
        try {
            File licFile = new File(licenseFile);
            if(licFile.exists()){
                licFile.delete();
            }else {
                File parentLicFile = licFile.getParentFile();
                if(!parentLicFile.exists()){
                    parentLicFile.mkdirs();
                }
            }
            file.transferTo(licFile);
            Map<String,String> map = YanzhengService.getYanzhengma(licFile);
            return AjaxResult.success(map);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return AjaxResult.error("授权失败="+e.getMessage());
        }
    }


    @PostMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam("type")Integer type) {
        try {
            CreateZhucema createZhucema=new CreateZhucema();
            Map<String, String> zhucemaNew = createZhucema.getZhucema("yqda_zcm_QWERGFD");
            String content = JSONObject.toJSONString(zhucemaNew);
            log.info(content);
            if(type == 1){
                ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
                GenerateQR.code(content,pngOutputStream);
                // 创建响应头
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=registration.png");
                headers.add(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE);
                // 返回 ResponseEntity
                return new ResponseEntity<>(pngOutputStream.toByteArray(), headers, HttpStatus.OK);
            }else {
                // 创建响应头
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=registration.txt");
                headers.add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
                // 返回 ResponseEntity
                return new ResponseEntity<>(content.getBytes(), headers, HttpStatus.OK);
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
